package com.net.wanjian.networkhospitalmanager.activity.evaluate;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.base.BaseActivity;
import com.net.wanjian.networkhospitalmanager.adapter.base.BaseFragmentPagerListAdapter;
import com.net.wanjian.networkhospitalmanager.bean.evaluate.SearchHistoryEventTypeResult;
import com.net.wanjian.networkhospitalmanager.consts.ErrorCodeReturn;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.observers.DisposableObserver;

public class HistoryEventEvaluateActivity extends BaseActivity {


    @BindView(R.id.top_back_text_tv)
    TextView topBackTextTv;
    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;
    @BindView(R.id.top_title_tv)
    TextView topTitleTv;
    @BindView(R.id.event_evaluate_tablayout)
    TabLayout eventEvaluateTablayout;
    @BindView(R.id.event_evaluate_viewpager)
    ViewPager eventEvaluateViewpager;

    private List<Fragment> mFragments = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();
    private TextView unreadView;

    @Override
    protected void initData() {
        topTitleTv.setText("历史事件");
        topBackLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HistoryEventEvaluateActivity.this.finish();
            }
        });

        //获取标签接口数据
        getTabTextHttpRequest();
    }

    //获取标签接口数据
    private void getTabTextHttpRequest() {
        SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();
        HttpUtil.searchHistoryEventType(xmlUtil.getDeviceId(), xmlUtil.getToken(), xmlUtil.getHospitalId(), new DisposableObserver<SearchHistoryEventTypeResult>() {
            @Override
            public void onNext(SearchHistoryEventTypeResult searchHistoryEventTypeResult) {
                if (searchHistoryEventTypeResult.getResult() == ErrorCodeReturn.success) {
                    initTabTypeTag(searchHistoryEventTypeResult.getData().getEventExtendTypeList());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
            }
        });
    }

    //加载标签
    private void initTabTypeTag(List<SearchHistoryEventTypeResult.DataBean.EventExtendTypeListBean> d) {
        int length = d.size();
        for (int i = 0; i < length; i++) {
            EvaluateListFragment fragment = EvaluateListFragment.newInstance(URLDecoderUtil.getDecoder(d.get(i).getEventExtendType()));
            mFragments.add(fragment);
        }

        BaseFragmentPagerListAdapter pagerListAdapter = new BaseFragmentPagerListAdapter(getSupportFragmentManager(), mFragments);
        eventEvaluateViewpager.setOffscreenPageLimit(length);
        eventEvaluateViewpager.setAdapter(pagerListAdapter);

        eventEvaluateTablayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        eventEvaluateTablayout.setupWithViewPager(eventEvaluateViewpager);

        int count = pagerListAdapter.getCount();
        for (int i = 0; i < count; i++) {
            TabLayout.Tab tab = eventEvaluateTablayout.getTabAt(i);//获得每一个tab
            tab.setCustomView(R.layout.tab_text_layout);//给每一个tab设置view
            TextView nameView = (TextView) tab.getCustomView().findViewById(R.id.tab_text_tv);
            if (i == count - 1){
                unreadView = (TextView) tab.getCustomView().findViewById(R.id.tab_unread_num_tv);
                unreadView.setVisibility(View.GONE);
            }else {
                TextView readView = (TextView) tab.getCustomView().findViewById(R.id.tab_unread_num_tv);
                readView.setVisibility(View.GONE);
            }


            nameView.setText(URLDecoderUtil.getDecoder(d.get(i).getEventExtendTypeName()));


            if (i == 0) {
                nameView.setTextColor(Color.parseColor("#209d99"));
            }

        }

        eventEvaluateTablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                eventEvaluateViewpager.setCurrentItem(tab.getPosition());
                ((TextView) tab.getCustomView().findViewById(R.id.tab_text_tv)).setTextColor(Color.parseColor("#209d99"));
            }

            @SuppressLint("ResourceAsColor")
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ((TextView) tab.getCustomView().findViewById(R.id.tab_text_tv)).setTextColor(R.color.colorMyName);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_history_event_evaluate;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    //提供给fragment调用设置待评价数量
    public void setWaitEvaluateView(int simple,int deep){
        int count = simple + deep;
        if (count > 0){
            unreadView.setVisibility(View.VISIBLE);
            unreadView.setText(count+"");
        }else {
            unreadView.setVisibility(View.GONE);
        }
    }
}
